<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/18
 * Time: 10:02
 */

namespace app\zjs\controller;
use think\Controller;
use think\facade\Session;
use think\Db;
use think\facade\Cache;

class Zjs extends Controller
{
    protected function _initialize()
    {
        $this->checkLogin();
        $this->getNav();
        $this->getSlide();
        $this->getTitle();
        $this->assign('action',$_SERVER["REQUEST_URI"]);
    }

    /**
     * 获取前端导航列表
     */
    protected function getNav()
    {
//        if (Cache::has('nav')) {
//            $nav = Cache::get('nav');
//        } else {
        $nav = Db::name('nav')->where(['status' => 1])->order(['sort' => 'ASC'])->select();
        $nav = !empty($nav) ? array2tree($nav) : [];
//            if (!empty($nav)) {
//                Cache::set('nav', $nav,600);
//            }
//        }
        $this->assign('nav', $nav);
    }
    /**
     * 获取前端导航列表的标题
     */
    protected function getTitle()
    {
        $menu_id = input('menu_id');
        if(!empty($menu_id)){
            if (Cache::has('titles'.$menu_id)) {
                $nav = Cache::get('titles'.$menu_id);
            } else {
                $nav = Db::name('nav')->where(['status' => 1,'id'=>$menu_id])->order(['sort' => 'ASC'])->find();
                if (!empty($nav)) {
                    Cache::set('titles'.$menu_id, $nav,600);
                }
            }
            $this->assign('title', $nav['name']);
        }

    }
    /**
     * 获取前端轮播图
     */
    protected function getSlide()
    {
        if (Cache::has('slide')) {
            $slide = Cache::get('slide');
        } else {
        $slide = Db::name('slide')->where(['status' => 1, 'cid' => 1])->order(['sort' => 'DESC'])->select();
            if (!empty($slide)) {
                Cache::set('slide', $slide,600);
            }
        }

        $this->assign('slide', $slide);
    }
    protected function checkLogin(){
        if (!session('user_zjs')) {
            $this->redirect('login/index');
        }
    }
}